package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.constant.CacheConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.domain.User;
import cn.itsource.ymcc.errorcode.ErrorCode;
import cn.itsource.ymcc.feignclient.LoginFeignClient;
import cn.itsource.ymcc.mapper.UserMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IUserService;
import cn.itsource.ymcc.util.AssertUtil;
import cn.itsource.ymcc.vo.UserRegisterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author Mr.xiang
 * @since 2023-09-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginFeignClient loginFeignClient;
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public JsonResult register(UserRegisterVo userRegisterVo) {
        //校验手机号是否注册
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, userRegisterVo.getMobile()));
        AssertUtil.isNull(user, ErrorCode.PHONE_REGISTERED.getMessage());

        //校验手机号验证码
        Boolean aBoolean = redisTemplate.hasKey(CacheConstants.PHONE_CODE_KEY + userRegisterVo.getMobile());
        AssertUtil.isTrue(aBoolean,ErrorCode.IMG_CODE_EXPIRED.getMessage());
        String code =  redisTemplate.opsForValue().get(CacheConstants.PHONE_CODE_KEY + userRegisterVo.getMobile()).toString();
        AssertUtil.isTrue(userRegisterVo.getSmsCode().equalsIgnoreCase(code),ErrorCode.PHONE_CODE_INPUT_ERROR.getMessage());

        //保存数据
        //1.先保存至登录信息表
        Login login = new Login();
        login.setUsername(userRegisterVo.getMobile());
        login.setEnabled(RConst.Num.INT_1);
        //加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(userRegisterVo.getPassword());
        login.setPassword(encode);
        login.setType(userRegisterVo.getRegChannel());

        JsonResult jsonResult = loginFeignClient.saveOrUpdate(login);
        //如果数据是Long类型，会自动变为Integer
        Integer loginId = (Integer) jsonResult.getData();
        User newUser = new User();
        newUser.setCreateTime(new Date());
        newUser.setPhone(userRegisterVo.getMobile());
        newUser.setLoginId(loginId.longValue());
        newUser.setNickName(userRegisterVo.getMobile());

        userMapper.insert(newUser);

        return JsonResult.success();
    }
}
